<?php
namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "friend_links".
 *
 * @property integer $id
 * @property string $name
 * @property string $link_url
 * @property integer $sort
 */
class FriendLinks extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'friend_links';
    }

    /*
     * 产生创建日期和更新日期时间戳
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'name',
                    'link_url',
                    'sort'
                ],
                'required'
            ],
            [
                [
                    'sort',
                    'created_at',
                    'updated_at'
                ],
                'integer'
            ],
            [
                [
                    'name',
                    'link_url'
                ],
                'string',
                'max' => 255
            ],
            [
                [
                    'name'
                ],
                'unique'
            ],
            [
                'link_url',
                'match',
                'pattern' => "/\b(?:(?:https?):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",
                'message' => '链接URL不是一个有效的URL.'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'link_url' => '链接Url',
            'sort' => '排序值',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At'
        ];
    }

    /**
     * @inheritdoc
     *
     * @return FriendLinksQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new FriendLinksQuery(get_called_class());
    }
}
